<?php

namespace App\Model;

//use Illuminate\Database\Eloquent\Model;

class Permission extends Model
{
    public $dates = ['deleted_at'];
    public $fillable = ['id','action','name','created_at','updated_at'];

    static public function _validate(){
        return array(
            'name'=>'required|min:2|max:64',
            'action'=>'required|min:2|max:64|unique:permissions'
        );
    }

    static public function permission_create($data){
        $data = self::validate($data);
        if(!$data['status']){
            return $data;
        }
        $data = $data['data'];
        $permission = self::create($data);
        if(isset($permission->id)){
            return Json::success($permission);
        }
        return Json::error('添加出错');

    }



    static public function Adminall($admin){
        // id 等于 1 的 超级管理员 获取所有的 权限id
        if($admin['id'] == 1){
            $permission = self::all()->toArray();
            foreach($permission as $val){
                $ids[$val['id']] = $val;
            }
            return $ids;
        }
        // 其他 用户 获取所有的 角色 id
        foreach($admin['role'] as $val){
            $role[] = $val['role'];
        }
        // 搜索 角色关联 权限 的方法
        $role = RolePermission::whereIn('id',$role)->group('permission')->get()->toArray();
        // 遍历出 id
        foreach($role as $val){
            $permission[$val['permission']] = $val;
        }
        return $permission;
    }
}
